package com.sghd.candy.module.system.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author frank
 * @date 2019-02-22 11:05
 * @desc
 **/
@Component("initCacheService")
public class CacheService implements ApplicationContextAware {

    private Set<CacheLife> lifeControls = new HashSet<>();
    private ApplicationContext applicationContext;

    @PostConstruct
    void init() {
        Map<String, CacheLife> managers = applicationContext.getBeansOfType(CacheLife.class);
        for (CacheLife lifeControl : managers.values()) {
            lifeControls.add(lifeControl);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void writeBack(Object id) {
        for (CacheLife lifeControl : lifeControls) {
            lifeControl.writeBack(id);
        }
    }
}
